#include "bigintptr.h"
#include "bigint.h"
#include <algorithm>
#include <iostream>
BigIntPtr::BigIntPtr(BigInt * num)
{
	if(num == 0) return;
	counter = new PtrCounter(num);
	counter -> Inc();
}
BigIntPtr::BigIntPtr(BigIntPtr const & p)
{
	counter = p.counter;
	counter -> Inc();
}
BigIntPtr::~BigIntPtr()
{
	std::cout << "I am deliting " << counter -> qty << "\n";
	counter -> Dec();
	
	if(counter -> isZero())
	{
		std::cout << " ---- I am delited counter " << counter -> qty << "\n";
		delete counter;
	}
}
void BigIntPtr::Swap(BigIntPtr & p)
{
	std::cout << "    in swap" << p.UseCount() << "\n";
	std::swap((*this).counter, p.counter);
	
}
BigInt * BigIntPtr::get() const
{
	return counter -> ptr;
}
int BigIntPtr::UseCount() const
{
	return counter -> qty;
}
BigIntPtr & BigIntPtr::operator =(BigIntPtr const & p)
{
	if(this != &p)
	{
		BigIntPtr(p).Swap(*this);
	}
	return *this;
}
BigInt * BigIntPtr::operator ->() const
{
	return counter -> ptr;
}
BigInt & BigIntPtr::operator *() const
{
	return (*(*counter).ptr);
}
bool operator ==(BigIntPtr const & p, BigIntPtr const & q)
{
	return p.get() == q.get();
}
bool operator !=(BigIntPtr const & p, BigIntPtr const & q)
{
	return !( p == q);
}
bool operator <(BigIntPtr const & p, BigIntPtr const & q)
{
	return p.get() < q.get();
}
bool operator >(BigIntPtr const & p, BigIntPtr const & q)
{
	return q < p;
}
//------PtrCounter-------------------------//
PtrCounter::PtrCounter(BigInt * num)
	:ptr(num),
	 qty(0)
{
}
PtrCounter::~PtrCounter()
{
	qty = 0;
	delete ptr;
}
void PtrCounter::Dec()
{
	--qty;
}
void PtrCounter::Inc()
{
	++qty;
}
bool PtrCounter::isZero()
{
	if( qty == 0) 
	{
		return true;
	}
	return false;
}
